package manager

import android.content.ContentResolver
import app.AppContext
import com.hontech.kdxclient.R
import event.AdvChangedEvent
import ext.log
import mvc.model.MediaData
import mvc.model.PictureData
import mvc.model.VideoData
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

object AdvertisingManager : Runnable {
    private val MediaListTop = ArrayList<MediaData>()
    private val MediaListBottom = ArrayList<MediaData>()
    private val Logo: PictureData
    var TopIndex = 0
    var BottomIndex = 0

    init {
        val r = AppContext.resources
        val url =
            ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + r.getResourcePackageName(R.drawable.ic_logo) + "/" + r.getResourceTypeName(
                R.drawable.ic_logo
            ) + "/" + r.getResourceEntryName(R.drawable.ic_logo)
        Logo = PictureData(url, Long.MAX_VALUE / 10, "00:00", "23:59")
    }

    fun findBottomNextMedia(): MediaData {
        if (BottomIndex >= MediaListBottom.size) {
            BottomIndex = 0
        }

        for (i in BottomIndex until MediaListBottom.size) {
            val info = MediaListBottom[i]
            if ((!info.isError) && info.isExist()) {
                BottomIndex = i + 1
                return info
            }
        }

        for (i in 0 until BottomIndex) {
            val info = MediaListBottom[i]
            if ((!info.isError) && info.isExist()) {
                BottomIndex = i + 1
                return info
            }
        }

        return Logo
    }

    fun findTopNextMedia(): MediaData {
        if (TopIndex >= MediaListTop.size) {
            TopIndex = 0
        }

        for (i in TopIndex until MediaListTop.size) {
            val info = MediaListTop[i]
            if ((!info.isError) && info.isExist()) {
                TopIndex = i + 1
                return info
            }
        }

        for (i in 0 until TopIndex) {
            val info = MediaListTop[i]
            if ((!info.isError) && info.isExist()) {
                TopIndex = i + 1
                return info
            }
        }

        return Logo
    }

    private fun refreshWithServer() {
        MediaListTop.clear()
        MediaListBottom.clear()
        TopIndex = 0
        BottomIndex = 0

        val tmp = HttpRequestManager.refreshAdvWithServer()

        log("广告:$tmp")
        val jsonObject = JSONObject(tmp)
        val arr = jsonObject.getJSONArray("adviertisementList")
        val len = arr.length()
        for (i in 0 until len) {
            val json = arr.optJSONObject(i)
            val start = json.optString("startTime")
            val end = json.optString("endTime")
            val type = json.optString("type")
            val position = json.optString("advertPosition")

            if (type == "image") {
                val url = json.optString("picOrvidUrl")
                val duration = json.optInt("duration")
                val pic = PictureData(url, duration * 1000L, start, end)
                if (position == "1") {
                    MediaListTop.add(pic)
                } else if (position == "2" || position == "3") {
                    MediaListBottom.add(pic)
                }
            } else if (type == "video") {
                val url = json.optString("picOrvidUrl")
                val video = VideoData(url, start, end)
                if (position == "1") {
                    MediaListTop.add(video)
                } else if (position == "2" || position == "3") {
                    MediaListBottom.add(video)
                }
            }
        }

        if (MediaListBottom.isNotEmpty() || MediaListTop.isNotEmpty()) {
            AppManager.advertising = tmp
            EventBus.getDefault().post(AdvChangedEvent())
        }
    }

    fun refreshWithAsync() {
        TaskManager.DelayHandler.post(this)
    }

    override fun run() {
        try {
            refreshWithServer()
        } catch (e: Exception) {
            e.printStackTrace()
            TaskManager.DelayHandler.postDelayed(this, 1000)
        }
    }
}